package com.ccl.iot.dvr.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.ccl.iot.App;
import com.ccl.iot.R;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.publics.Messager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Math.PI;
import static java.lang.Math.atan;

/**
 *
 */
public class DVRUtil {

    public static final int MODE_NONE = -1;
    public static final int MODE_TILT_UP = 1;
    public static final int MODE_TILT_DOWN = 2;
    public static final int MODE_PAN_LEFT = 3;
    public static final int MODE_PAN_RIGHT = 4;
    public static final int MODE_UP_LEFT = 5;
    public static final int MODE_UP_RIGHT = 6;
    public static final int MODE_DOWN_LEFT = 7;
    public static final int MODE_DOWN_RIGHT = 8;
    public static final int MODE_ZOOM_IN = 9;
    public static final int MODE_ZOOM_OUT = 10;

    public static final int PREVIEW_SMOOTH = 1;
    public static final int PREVIEW_HIGH_DEFINITION = 0;

    public static final int MODE_NO_INIT_DB = -1;
    public static final int MODE_CONTROL = 0;
    public static final int MODE_DVR = 1;
    public static final int MODE_CAMERA = 2;

    public static final int ERROR_LOGIN = 1;
    public static final int ERROR_REALPLAY = 2;
    public static final int ERROR_GETPORT = 3;
    public static final int ERROR_SETSTREAMOPENMODE = 4;
    public static final int ERROR_OPENSTREAM = 5;
    public static final int ERROR_PLAY = 6;
    public static final int ERROR_DVR_PASSWORD_ERROR = 7;


    public static final int DVR_NO_INIT_DB = -1;
    public static final int DVR_NO_DATA = 0;
    public static final int DVR_NONE = 1000;
    public static final int DVR_HIKV = 1001;
    public static final int DVR_YS = 1002;

    public static final int NO_ZONE_INDEX = -1;

    public static final String DEVICEINFO = "deviceInfo";
    public static final String CAMERANO = "camerano";

    public static final String YS_USER = "ys_user";
    public static final String YS_PWD = "ys_pwd";

    public static int mHiKvVideoLevel = 1;

    public static int mYSVideoLevel = 1;

    private static int mMoveMode = -1;

    public static long mNetWorkSpeed;

//    public static String mIP = "192.168.100.100";
//    public static String mPwd = "12345";
//    public static String mUser = "admin";
//    public static int mPort = 8022;

    public static String mIP = null;
    public static String mPwd = null;
    public static String mUser = null;
    public static int mPort = 0;

    public static String mYSUser = null;
    public static String mYSPwd = null;

    public static IOTDevice mCurrentDevice;
    public static int mCurrentDeviceID;
    public static int zoneIndex;

    public static boolean isDVR;


    /**
     * 海康账号是否登录中
     */
    public static boolean isHiKvLogining;

    public static boolean hasZero;

    /**
     * 通道号和通道名字的map
     */
    public static Map<Integer,String> mChannelMap= new HashMap<Integer, String>();

    /**
     * 获取两个坐标点的角度
     *
     * @param pointTo
     * @param pointFrom
     * @return
     */
    public static int getAngle(PointF pointTo, PointF pointFrom) {
        double dAngle = 0;
        if (!pointTo.equals(pointFrom)) {
            // 求角度,向右方向为x正方向,向下方向为y正方向
            double dX = pointTo.x - pointFrom.x;
            double dY = pointTo.y - pointFrom.y;

            // 1
            if (dX > 0 && dY > 0) {
                dAngle = PI / 2 - atan(dY / dX);
            }
            // 2
            if (dX < 0 && dY > 0) {
                dAngle = 2 * PI - (PI / 2 + atan(dY / dX));
            }
            // 3
            if (dX < 0 && dY < 0) {
                dAngle = 2 * PI - (PI / 2 + atan(dY / dX));
            }
            // 4
            if (dX > 0 && dY < 0) {
                dAngle = PI / 2 - atan(dY / dX);
            }

            // 5
            if (dX == 0 && dY > 0) {
                dAngle = 0;
            }
            // 6
            if (dX == 0 && dY < 0) {
                dAngle = PI;
            }
            // 7
            if (dY == 0 && dX > 0) {
                dAngle = PI / 2;
            }
            // 8
            if (dY == 0 && dX < 0) {
                dAngle = PI * 1.5;
            }

            dAngle = dAngle * 180 / PI - 90;

            while (dAngle > 360)
                dAngle -= 360;
            while (dAngle < 0)
                dAngle += 360;
        }
        return (int) dAngle;
    }

    /**
     * 计算两个手指间的距离
     *
     * @param event
     * @return
     */
    public static float distance(MotionEvent event) {
        if (event.getPointerCount() >= 2) {
            float dx = event.getX(1) - event.getX(0);
            float dy = event.getY(1) - event.getY(0);
            /** 使用勾股定理返回两点之间的距离 */
            return (float) Math.sqrt(dx * dx + dy * dy);
        }
        return 0;
    }

    /**
     * 获取两点之间距离
     *
     * @param x1 点1
     * @param y1 点1
     * @param x2 点2
     * @param y2 点2
     * @return 距离
     */
    public static float getDistance(float x1, float y1, float x2, float y2) {
        float x = x1 - x2;
        float y = y1 - y2;
        return (float) Math.sqrt(x * x + y * y);
    }

    /**
     * 获取摄像头的移动模式
     *
     * @param angle
     */
    public static int getMoveMode(double angle) {

        // 正常的方向
        // if ((angle >337.5f && angle <= 360f) || (angle > 0 && angle <=
        // 22.5f)) {
        // mMoveMode = MODE_PAN_RIGHT;
        // } else if (angle > 22.5 && angle <= 67.5f) {
        // mMoveMode = MODE_UP_RIGHT;
        // } else if (angle > 67.5f && angle <= 112.5f) {
        // mMoveMode = MODE_TILT_UP;
        // } else if (angle > 112.5f && angle <= 157.5f) {
        // mMoveMode = MODE_UP_LEFT;
        // } else if (angle > 157.5f && angle <= 202.5f) {
        // mMoveMode = MODE_PAN_LEFT;
        // } else if (angle > 202.5f && angle <= 247.5f) {
        // mMoveMode = MODE_DOWN_LEFT;
        // } else if (angle > 247.5f && angle <= 292.5f) {
        // mMoveMode = MODE_TILT_DOWN;
        // } else if (angle > 292.5f && angle <= 337.5f) {
        // mMoveMode = MODE_DOWN_RIGHT;
        // }

        // 手指移动方向的反方向
        // if ((angle > 337.5f && angle <= 360f) || (angle > 0 && angle <=
        // 22.5f)) {
        // mMoveMode = MODE_PAN_LEFT;
        // } else if (angle > 22.5 && angle <= 67.5f) {
        // mMoveMode = MODE_DOWN_LEFT;
        // } else if (angle > 67.5f && angle <= 112.5f) {
        // mMoveMode = MODE_TILT_DOWN;
        // } else if (angle > 112.5f && angle <= 157.5f) {
        // mMoveMode = MODE_DOWN_RIGHT;
        // } else if (angle > 157.5f && angle <= 202.5f) {
        // mMoveMode = MODE_PAN_RIGHT;
        // } else if (angle > 202.5f && angle <= 247.5f) {
        // mMoveMode = MODE_UP_RIGHT;
        // } else if (angle > 247.5f && angle <= 292.5f) {
        // mMoveMode = MODE_TILT_UP;
        // } else if (angle > 292.5f && angle <= 337.5f) {
        // mMoveMode = MODE_UP_LEFT;
        // }

        // 正常的4个方向
        // if (angle >= 45.0f && angle < 135.0f) {
        // mMoveMode = MODE_TILT_UP;
        // } else if (angle >= 225.0f && angle < 315.0f) {
        // mMoveMode = MODE_TILT_DOWN;
        // } else if (angle >= 135.0f && angle < 225.0f) {
        // mMoveMode = MODE_PAN_LEFT;
        // } else if ((angle >= 315f && angle <= 360f)
        // || (angle > 0 && angle < 45f)) {
        // mMoveMode = MODE_PAN_RIGHT;
        // }

        // 反方向的4个方向
        if (angle >= 45.0f && angle < 135.0f) {
            mMoveMode = MODE_TILT_DOWN;
        } else if (angle >= 225.0f && angle < 315.0f) {
            mMoveMode = MODE_TILT_UP;
        } else if (angle >= 135.0f && angle < 225.0f) {
            mMoveMode = MODE_PAN_RIGHT;
        } else if ((angle >= 315f && angle <= 360f) || (angle > 0 && angle < 45f)) {
            mMoveMode = MODE_PAN_LEFT;
        }

        return mMoveMode;
    }

    public static List<Integer> mStartChanList = new ArrayList<Integer>();

    private static AlertDialog mChannelDialog;

//    public static void changeChannelDialog(Context context) {
//        if (Consts.mChannelCount > 1) {
//            // 当监控设备有多个通道时
//            if (mChannelDialog != null && mChannelDialog.isShowing()) {
//                mChannelDialog.dismiss();
//                mChannelDialog = null;
//            }
//
//            final String[] mChannelItems = Consts.mChannelItems;
//            AlertDialog.Builder builder = new AlertDialog.Builder(context); // 先得到构造器
//            builder.setTitle(R.string.select_channel); // 设置标题
//            builder.setIcon(R.drawable.icon_dvr_white);// 设置图标，图片id即可
//            builder.setItems(mChannelItems, new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//
//                    if (mChannelItems != null && mChannelItems.length > 1) {
//                        if (which != mStartChanList.size() - 1) {
//                            int channelNo = mStartChanList.get(which);
//                            EventBus.getDefault().post(new ChannelEvent(channelNo));
//                        } else {
//                            // 零通道
//                            EventBus.getDefault().post(new ChannelEvent(0));
//                        }
//                    }
//                }
//            });
//
//            mChannelDialog = builder.create();
//            mChannelDialog.show();
//
//        } else {
//            // 当监控设备有1个通道时
//            EventBus.getDefault().post(new ChannelEvent(1));
//        }
//
//    }

    /**
     * 拼接ID
     *
     * @param device
     * @return ID(long)
     */
    public static long getDeviceID(IOTDevice device) {
        if (device != null) {
            long ID = device.getID() * 100;
            int address = device.getRegisterAddress();
            return ID + address;
        }
        return 0;
    }

    /**
     * 拼接ID
     *
     * @param device
     * @return ID(String)
     */
    public static String getDeviceIDStr(IOTDevice device) {
        if (device != null) {
            long ID = device.getID() * 100;
            int address = device.getRegisterAddress();
            long deviceID = ID + address;
            return String.valueOf(deviceID);
        }
        return null;
    }

    /**
     * 提示信息
     */
    public static void sendToast() {
        Messager.Show(App.getContext().getResources().getString(R.string.error_dvr));
    }

    /**
     * 提示信息
     */
    public static void sendToast(String msg) {
        Messager.Show(msg);
    }

    /**
     * 提示信息
     * @param context
     * @param error
     */
    public static void sendToast(Context context,int error) {
        switch (error) {
            case DVRUtil.ERROR_LOGIN:
                sendToast(context.getString(R.string.error_login));
                break;
            case DVRUtil.ERROR_REALPLAY:
                sendToast(context.getString(R.string.error_realplay));
                break;
            case DVRUtil.ERROR_GETPORT:
                sendToast(context.getString(R.string.error_getport));
                break;
            case DVRUtil.ERROR_SETSTREAMOPENMODE:
                sendToast(context.getString(R.string.error_setstreamopenmode));
                break;
            case DVRUtil.ERROR_OPENSTREAM:
                sendToast(context.getString(R.string.error_openstream));
                break;
            case DVRUtil.ERROR_PLAY:
                sendToast(context.getString(R.string.error_play));
                break;
            case DVRUtil.ERROR_DVR_PASSWORD_ERROR:
                sendToast(context.getString(R.string.error_dvr_password_error));
                break;
            default:
                break;
        }

    }



}
